package com.muslimedia.reloadmanager;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;

import com.muslimedia.reloadmanager.adapter.MenuAdapter;
import com.muslimedia.reloadmanager.data.CustomerDataSource;
import com.muslimedia.reloadmanager.model.Menu;

public class Home extends Activity {
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_home);

	    GridView gridview = (GridView) findViewById(R.id.gridview);
	    gridview.setAdapter(new MenuAdapter(this, getMenuModel()));

	    gridview.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView<?> parent, View v, 
	        		int position, long id) {
	        	List<Menu> menus = getMenuModel();
	        	Menu m = menus.get(position);
	            Toast.makeText(Home.this, m.getTitle(), Toast.LENGTH_SHORT).show();
	            Intent intent = new Intent(Home.this, m.getClassTarget());
	    	    
	            if(m.getThumb() == R.drawable.ic_launcher) {
	            	String message = "message";
	            	final EditText input = new EditText(Home.this);
	            	new AlertDialog.Builder(Home.this)
	                .setTitle("Update Status")
	                .setMessage(message)
	                .setView(input)
	                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                        Editable value = input.getText();
	                        long l = Long.parseLong(value.toString()); 
	                        CustomerDataSource c = new CustomerDataSource(Home.this);
	                        c.isMemberGroup(l);
	                    }
	                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                        // Do nothing.
	                    }
	                }).show();
	            }
	            else {
	            	CustomerDataSource c = new CustomerDataSource(Home.this);
	            	c.getGroups();
	            }
	            
	            //startActivity(intent);
	        }
	    });
	}
	
	private List<Menu> getMenuModel(){
		List<Menu> menus = new ArrayList<Menu>();
		menus.add(new Menu(R.drawable.ic_launcher, "Get Group", ReloadTransaction.class));
		menus.add(new Menu(R.drawable.ic_splash, "Service", ReloadService.class));
		
		return menus;
	}
}